package handlers

import (
	"git.hortorgames.com/serverless/htgserver/apiserver"
	"git.hortorgames.com/serverless/htgserver/apiserver/session"
	"git.hortorgames.com/serverless/htgserver/consts"
)

type MidAntiResend struct {
	Base
}

func NewMidAntiResend(server *apiserver.Server) *MidAntiResend {
	h := &MidAntiResend{}
	h.Server = server
	return h
}

//@httpMid:""
func (h *MidAntiResend) Run(ctx *apiserver.Context) {
	ctx.AntiResend = true
	sess := ctx.StartRedisSession()
	defer sess.End()
	bytes, _ := ctx.GetRawData()
	resp, err := session.AddReq(sess, ctx.RequestData.PlayerId, ctx.RequestData.Seq, bytes)
	if err != nil {
		ctx.Logger().Error("AddReq error", "err", err.Error())
		h.Halt(ctx, consts.ErrorInternalError)
		ctx.Abort()
		return
	}
	if resp != nil {
		ctx.Logger().Debug("Resend resp")
		h.Server.RespRaw(ctx, resp)
		ctx.Abort()
		return
	}
	ctx.Next()
}
